package com.huali.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "CookieServlet", value = "/cookie")
public class CookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理响应编码
        response.setContentType("text/html;charset=UTF-8");
        //获取所有cookie
        Cookie[] cookies = request.getCookies();
        //inn(判断不为null)   ifn(判断为null)
        if (cookies != null) {
            //iter
            for (Cookie cookie : cookies) {
                System.out.println("cookie键:" + cookie.getName() + " cookie值:" + cookie.getValue());
            }
        }

        //获取session中的数据
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        System.out.println("session中的数据:" + user);


        response.getWriter().write("服务器响应成功！！！");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
